package com.yuan.util.image;

import com.yuan.util.file.FileUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 去除验证码干扰线
 * @author YouYuan
 * @version 1.0
 * @CreateDate 2018/5/14 17:52
 * @UpdateDate 2018/5/14 17:52
 */
public class CleanLines {

    /**
     * 默认图片处理阈值
     */
    public static final int IMG_PROCESS_THRESHOLD = 50;

    public static void main(String[] args) throws IOException {
        File testDataDir = new File("D:\\Temp\\Develop\\Img\\20180524");
        String destDir = testDataDir.getAbsolutePath() + "/tmp-2/";
        for (File file : testDataDir.listFiles()) {
            if (file.isFile()) {
                cleanLines(file, new File(destDir), 200);
            }
        }
    }

    public static boolean cleanLines(String imgFilePath, String outPath, int threshold) throws IOException {
        return cleanLines(new File(imgFilePath), new File(outPath), threshold);
    }

    public static boolean cleanLines(String imgFilePath, String outPath) throws IOException {
        return cleanLines(imgFilePath, outPath, IMG_PROCESS_THRESHOLD);
    }

    public static BufferedImage cleanLines(String imgFilePath, int threshold) throws IOException {
        return cleanLines(new File(imgFilePath), threshold);
    }

    public static BufferedImage cleanLines(String imgFilePath) throws IOException {
        return cleanLines(new File(imgFilePath), IMG_PROCESS_THRESHOLD);
    }
    public static BufferedImage cleanLines(File imgFile) throws IOException {
        return cleanLines(imgFile, IMG_PROCESS_THRESHOLD);
    }

    public static BufferedImage cleanLines(File imgFile, int threshold) throws IOException {
        BufferedImage img = ImageIO.read(imgFile);
        int width = img.getWidth();
        int height = img.getHeight();
        int minRgb = 999;
        int secondRgb = 999;
        for (int x = 0; x < width; x++){
            for (int y = 0; y < height; y++){
                Color color = new Color(img.getRGB(x, y));
                int num = color.getRed()+color.getGreen()+color.getBlue();
                if (num < minRgb ) {
                    secondRgb = minRgb;
                    minRgb = num;
                }
            }
        }
        int thresholdRgb = (minRgb + secondRgb) / 2 + threshold;
        for (int x = 0; x < width; x++){
            for (int y = 0; y < height; y++){
                Color color = new Color(img.getRGB(x, y));
                int num = color.getRed()+color.getGreen()+color.getBlue();
                if (num <= thresholdRgb) {
                    img.setRGB(x, y, Color.BLACK.getRGB());
                } else {
                    img.setRGB(x, y, Color.WHITE.getRGB());
                }
            }
        }
        return img;
    }

    public static boolean cleanLines(File imgFile, File outPath) throws IOException {
        return cleanLines(imgFile, outPath, IMG_PROCESS_THRESHOLD);
    }

    public static boolean cleanLines(File imgFile, File outFile, int threshold) throws IOException {
        FileUtil.createNewFile(outFile);
        BufferedImage img = cleanLines(imgFile, threshold);
//        File file = new File(outFile.getAbsolutePath() + "/" + imgFile.getName());
        return ImageIO.write(img, "jpg", outFile);
    }

}
